package ext.vpde.cadvaulting.migration;

import java.io.*;
import java.util.*;

import ext.vpde.cadvaulting.util.WINDSQL;
import wt.adapter.BasicWebjectDelegate;
import wt.epm.*;
import wt.fc.*;
import wt.vc.*;
import wt.series.*;
import wt.folder.Folder;
import wt.vc.wip.*;

public class ReviseEPMDocs {

	public static final String theSeries = "wt.series.HarvardSeries.FileBased";
	
	public static void main ( String [] args ) throws Exception {
		if (args.length != 3) {
			System.out.println("\nUsage: java.ext.vpde.cadvaulting.ReviseEPMDocs adminid adminpw loadfile");
		} else {
			if (LoadUtil.loginRemote(args[0],args[1])) {
				System.out.println("BEGIN REVISE EPM DOCS: " + Calendar.getInstance().getTime());
				WINDSQL.openConnection();
				BufferedReader file = LoadUtil.openInputFile(args[2]);
				if (file != null) {
					String s = file.readLine();
					int lineNumber = 0;
					while (s!= null && s.length() > 0) {
						LoadUtil.printProgress(++lineNumber);
						Hashtable ht = LoadUtil.processInputLine(s);
						String CADName = ht.get("CADNAME").toString();
						String docVer  = ht.get("VERSION").toString();
						String docIter = ht.get("ITERATION").toString();
		    			
						if (!WINDSQL.documentExists(CADName,docVer,docIter)) reviseDocument(ht);
						s = file.readLine();
					}
				}
				WINDSQL.closeConnection();
				System.out.println("-END- REVISE EPM DOCS: " + Calendar.getInstance().getTime());
			}
		}
    }
	
	private static boolean reviseDocument(Hashtable ht) throws Exception {
		//boolean reviseOK = false;
    	String CADName   = ht.get("CADNAME").toString();
    	String version   = ht.get("VERSION").toString();
    	String iteration = ht.get("ITERATION").toString();
    	String state     = ht.get("STATE").toString();
    	String docName   = ht.get("DOCNAME").toString();
    	int ftStatus     = Integer.parseInt(ht.get("FTSTATUS").toString());
    	String domainRef = WINDSQL.getDomainRef(CADName);
    	String ida2a2    = "";
    	
    	if (WINDSQL.documentExists(CADName, version, iteration)) {
    		return false;
    	}
    	
    	if (WINDSQL.documentExists(CADName,version)) {
    		// iterate
    		EPMDocument doc = (EPMDocument)BasicWebjectDelegate.getObjectByUfid(WINDSQL.getLatestDocument(CADName,version));
    		
    		
    		Folder folder = WorkInProgressHelper.service.getCheckoutFolder();
			Workable workable = (Workable)doc;
			CheckoutLink checkoutlink = WorkInProgressHelper.service.checkout(workable, folder, "");
			Workable workable1 = checkoutlink.getWorkingCopy();
			Persistable p = (Persistable)WorkInProgressHelper.service.checkin(workable1,"");
			ida2a2 = LoadUtil.getIdFromPersistable(p);
			if (ida2a2 != null) {
	    		WINDSQL.updateFamilyTableStatus(ida2a2,ftStatus);
	    		WINDSQL.updateIteration(ida2a2,iteration,domainRef);
	    		WINDSQL.updateState(ida2a2,state);
	    		WINDSQL.updateName(ida2a2,docName);
			}
			return true;
    	}
    	
    	EPMDocument doc = (EPMDocument)BasicWebjectDelegate.getObjectByUfid(WINDSQL.getLatestRevision(CADName));
    		Versioned newVer = VersionControlHelper.service.newVersion(doc);
			MultilevelSeries mls = MultilevelSeries.newMultilevelSeries(theSeries,version);
			newVer.setVersionInfo(VersionInfo.newVersionInfo(mls));
			Persistable p = PersistenceHelper.manager.save(newVer);
			ida2a2 = LoadUtil.getIdFromPersistable(p);
			if (ida2a2 != null) {
    			WINDSQL.updateFamilyTableStatus(ida2a2,ftStatus);
    			WINDSQL.updateIteration(ida2a2,iteration);
    			WINDSQL.updateState(ida2a2,state);
    			WINDSQL.updateName(ida2a2,docName);
			}
			return true;
	}
}